package codegenerator;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;

import baytony.util.log.Log;

public class CommonHelper {

	private static final Logger logger = Log.getLogger();
	private static Properties prop = new Properties();
	
	/**
	 * 讀取type.properties中的資料，裡面定義了資料庫型態與Java型態的對應
	 */
	static {
		try {
			FileInputStream fis = new FileInputStream(new File("src/type.properties"));
			prop.load(fis);
		} catch (FileNotFoundException e) {
			logger.error(e.getMessage());
			Log.logStackTrace(e);
		} catch (InvalidPropertiesFormatException e) {
			logger.error(e.getMessage());
			Log.logStackTrace(e);
		} catch (IOException e) {
			logger.error(e.getMessage());
			Log.logStackTrace(e);
		}
	}
	
	/**
	 * 取得傳入字串中符合的部分
	 *
	 * @param searchStr
	 * @param regExp
	 * @return
	 */
	public static List<String> getMatchStrs(String searchStr, String regExp) {
		List<String> result = null;
		Pattern pattern = Pattern.compile(regExp);
		Matcher match = pattern.matcher(searchStr);
		while(match.find()) {
			if(result == null)
				result = new ArrayList<String>();
			result.add(match.group());
		}
		return result;
	}
	
	/**
	 * 將 database type 轉換成 java type
	 *
	 * @param dbType
	 * @return
	 */
	public static String dbType2JavaType(String dbType) {
		return prop.getProperty(dbType.toLowerCase());
	}
	
	/**
	 * 把table name 改成 java bean name
	 *
	 * @param tableName
	 * @return
	 */
	public static String table2ClassName(String tableName) {
		StringBuffer result = new StringBuffer();
		String attrName = tableName.toLowerCase();
		String[] splitName = attrName.split("_");
		for(int i=0 ; i<splitName.length ; i++) {
			char c = splitName[i].charAt(0);
			String uc = String.valueOf(c).toUpperCase();
			String subs = splitName[i].substring(1);
			result.append(uc + subs);
		}
		return result.toString();
	}
	
	/**
	 * 把column name 轉成 attribute name
	 *
	 * @param columnName
	 * @return
	 */
	public static String column2Attribute(String columnName) {
		StringBuffer result = new StringBuffer();
		String attrName = columnName.toLowerCase();
		String[] splitName = attrName.split("_");
		for(int i=0 ; i<splitName.length ; i++) {
			if(i == 0) {
				result.append(splitName[i]);
			}
			else {
				char c = splitName[i].charAt(0);
				String uc = String.valueOf(c).toUpperCase();
				String subs = splitName[i].substring(1);
				result.append(uc + subs);
			}
		}
		return result.toString();
	}
	
	/**
	 * 把 column name 轉成 method name
	 *
	 * @param columnName
	 * @return
	 */
	public static String column2MethodName(String columnName) {
		return table2ClassName(columnName);
	}
}
